package com.sitecsys.controller;


import cn.hutool.core.io.resource.InputStreamResource;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LHB
 * @since 2024-01-06
 */
@RestController
@RequestMapping("/api/td3")
public class Td3Controller {

    private final RestTemplate restTemplate;

    public Td3Controller() {
        this.restTemplate = new RestTemplate();
    }

    @PostMapping("/send-params")
    public JSONObject sendParamsToDjango(@RequestBody String params) {
        RestTemplate restTemplate = new RestTemplate();
        String djangoApiUrl = "http://localhost:8000/generate_data/";
        JSONObject s = restTemplate.postForObject(djangoApiUrl, params, JSONObject.class);
        return s;
    }

    @PostMapping("/td3_lstm")
    public ResponseEntity<String> sendParamsToDjango2(@RequestBody String requestBody) {
        try {

            String djangoApiUrl = "http://localhost:8000/td3_lstm/";
            // 将请求体转发给Django
            ResponseEntity<String> responseFromDjango = restTemplate.postForEntity(djangoApiUrl, requestBody, String.class);

            // 将Django的响应转发给前端
            return ResponseEntity.ok(responseFromDjango.getBody());
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error forwarding request to Django");
        }
    }

    @PostMapping("/td3_no_lstm")
    public ResponseEntity<String> sendParamsToDjango3(@RequestBody String requestBody) {
        try {

            String djangoApiUrl = "http://localhost:8000/td3_no_lstm/";
            // 将请求体转发给Django
            ResponseEntity<String> responseFromDjango = restTemplate.postForEntity(djangoApiUrl, requestBody, String.class);

            // 将Django的响应转发给前端
            return ResponseEntity.ok(responseFromDjango.getBody());
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error forwarding request to Django");
        }
    }
}

